package com.juzipi.demo.controller;


import com.juzipi.demo.pojo.Dept;
import com.juzipi.demo.pojo.PageResult;
import com.juzipi.demo.pojo.Student;
import com.juzipi.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("stu")
public class StudentController {


    @Autowired
    private StudentService studentService;


    /**
     * 根据id查询所有学生
     * @param id
     * @return
     */
    //@PathVariable：获取路径中的参数
    @GetMapping("/{id}")
    public ResponseEntity<Student> queryStudentById( @PathVariable("id")Integer id){

        Student student = this.studentService.queryStudentById(id);
        //结果为空就返回404
        if (StringUtils.isEmpty(student)){
            return ResponseEntity.notFound().build();
        }
        //非空
        return ResponseEntity.ok(student);
    }


    /**
     * 新增学生
     * @param student
     * @return
     */
    @PostMapping
    //不需要返回值
    /*
    @RequestBody: 请求正文 接收的数据是json格式
    @RequestParam：这个传的是请求参数     举个例子： ?id=1&name=zhangsan&age=20 加在请求路径后面的
     */
    public ResponseEntity<Void> saveStudents(@RequestBody Student student){
        studentService.saveStudents(student);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 更新学生信息
     * @param student
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateStudents(@RequestBody Student student){
        studentService.updateStudents(student);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据id删除学生
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Integer> deleteStudentById(@PathVariable Integer id){
        studentService.deleteStudentById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据条件分页查询 student
     * @param key
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("page")
    public ResponseEntity<PageResult<Student>> queryStudentByPage(
            //@RequestParam: 将请求参数绑定到控制器的方法参数上
            //value:参数名
            //required：是否包含该参数，默认为true，表示该请求路径中必须包含该参数
            //defaultValue：默认参数值，设置了此值，required就会失效，自动为false，如果没有传该参数就会使用默认值
            @RequestParam(value = "key",required = false)String key, //搜索关键词，String
            @RequestParam(value = "page",defaultValue = "1")Integer page,//页码
            @RequestParam(value = "rows",defaultValue = "5")Integer rows //行数
    ){
        PageResult<Student> result = studentService.queryStudentByPage(key,page,rows);
        //判断结果是否为空，当前页数据是否为空
        if (result == null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(result);
    }


    /**
     * 根据排序条件查询返回排序分页结果集
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("pagesort")
    public ResponseEntity<PageResult<Student>> queryStudentSortByPage(
            @RequestParam(value = "key",required = false)String key,//搜索关键词，String
            @RequestParam(value = "page",defaultValue = "1")Integer page,//页码
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,//每页条数
            @RequestParam(value = "sortBy",required = false)String sortBy,//排序条件字段，例如传入age字段就会根据age排序
            @RequestParam(value = "desc",required = false)Boolean desc //是否降序，默认值为false
    ){
        PageResult<Student> studentPageResult = studentService.queryStudentSortByPage(key,page,rows,sortBy,desc);
        //根据结果集里的当前页数据判断它是否为空
        if (CollectionUtils.isEmpty(studentPageResult.getItems())){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(studentPageResult);
    }


    /**
     * 查询某一个系下的所有学生
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("dept")
    public ResponseEntity<Map<String,Object>> queryStudnetByDname(
            @RequestParam(value = "key") String key,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize
            ){

        Map<String,Object> depts = studentService.queryStudnetByDname(key,pageNum,pageSize);
        if (CollectionUtils.isEmpty(depts)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }

        return ResponseEntity.ok(depts);

    }



    @GetMapping("getDept")
    public ResponseEntity<List<Dept>> queryDept(){
        List<Dept> depts = studentService.queryDept();
        if (CollectionUtils.isEmpty(depts)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(depts);
    }


    @PostMapping("postDept")
    public ResponseEntity<PageResult<Dept>> queryDeptByPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize
    ){
        PageResult<Dept> pageResult = studentService.queryDeptByPage(key,pageNum,pageSize);
        if (CollectionUtils.isEmpty(pageResult.getItems())){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(pageResult);
    }





}
